package com.csd.view;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.csd.activity.R;
import com.csd.adapter.CarTypeAdapter;
import com.csd.bean.Car;


public class CarTypePopupwindow extends PopupWindow {

	private View conentView;
	private TextView text;
	private RadioGroup rg;
	private RadioButton rb_luxurious,rb_comfort,rb_economical;
	String typeName[] = {"所有车型","所有豪华型","现代伊兰特","别克凯越","大众朗逸","起亚K2","雪佛兰科鲁兹","马自达3"};
	int img[] = {0,0,R.drawable.u98,R.drawable.u98,R.drawable.u103,R.drawable.u120,R.drawable.u108,R.drawable.u125};
	
	String typeName1[] = {"所有车型","所有舒适型","别克凯越","起亚K2","雪佛兰科鲁兹","马自达3","大众朗逸","现代伊兰特"};
	int img1[] = {0,0,R.drawable.u98,R.drawable.u103,R.drawable.u120,R.drawable.u108,R.drawable.u103,R.drawable.u98};
	
	private List<Car> list;
	private GridView gv;

	public CarTypePopupwindow(final Activity context, final TextView text,final TextView v_text) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.pop_cartype, null);
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w - 32);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();

		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimationPreview);
		
		gv = (GridView) conentView.findViewById(R.id.gv);
		rb_luxurious = (RadioButton) conentView.findViewById(R.id.rb_luxurious);
		rb_economical = (RadioButton) conentView.findViewById(R.id.rb_economical);
		rb_comfort = (RadioButton) conentView.findViewById(R.id.rb_comfort);
		list = new ArrayList<>();
		for (int i = 0; i < img.length; i++) {
			Car carType = new Car();
			carType.setCarBrand(typeName[i]);
			carType.setImg(img[i]);
			list.add(carType);
		}
		CarTypeAdapter adapter = new CarTypeAdapter(list, context);
		gv.setAdapter(adapter);
		rg = (RadioGroup) conentView.findViewById(R.id.pop_rg);
		
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				switch (checkedId) {
				case R.id.rb_luxurious:
					list = new ArrayList<>();
					for (int i = 0; i < img.length; i++) {
						Car carType = new Car();
						carType.setCarBrand(typeName[i]);
						carType.setImg(img[i]);
						list.add(carType);
					}
					CarTypeAdapter adapter = new CarTypeAdapter(list, context);
					gv.setAdapter(adapter);
					break;
				case R.id.rb_comfort:
					list = new ArrayList<>();
					for (int i = 0; i < img1.length; i++) {
						Car carType = new Car();
						carType.setCarBrand(typeName1[i]);
						carType.setImg(img1[i]);
						list.add(carType);
					}
					CarTypeAdapter adapter1 = new CarTypeAdapter(list, context);
					gv.setAdapter(adapter1);
					break;
				case R.id.rb_economical:
					list = new ArrayList<>();
					for (int i = 0; i < img.length; i++) {
						Car carType = new Car();
						carType.setCarBrand(typeName[i]);
						carType.setImg(img[i]);
						list.add(carType);
					}
					CarTypeAdapter adapter2 = new CarTypeAdapter(list, context);
					gv.setAdapter(adapter2);
					break;
				default:
					break;
				}
			}
		});
		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				text.setText(list.get(position).getCarBrand());
				CarTypePopupwindow.this.dismiss();
				if(v_text != null){
					v_text.setVisibility(View.VISIBLE);
				}
			}
		});
	}

	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// 以下拉方式显示popupwindow
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 36);
		} else {
			this.dismiss();
		}
	}

}
